package com.llg.util;

import com.alibaba.fastjson.JSON;
import com.llg.model.BrandModel;
import com.llg.model.SpecModel;
import com.llg.pojo.LedBrand;
import com.llg.pojo.LedSpec;

import java.util.HashMap;

public class Entity2Model {

    public static BrandModel brand2Model(LedBrand brand){
        if(brand == null) return null;
        BrandModel brandModel = new BrandModel();
        brandModel.setId(brand.getId());
        brandModel.setId(brand.getId());
        brandModel.setName(brand.getName());
        String[] paras = brand.getOtherPara().split(",");
        brandModel.setOtherPara(paras);
        return brandModel;
    }

    public static LedBrand model2Brand(BrandModel brandModel){
        if(brandModel == null) return null;
        LedBrand brand = new LedBrand();
        brand.setId(brandModel.getId());
        brand.setName(brandModel.getName());
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < brandModel.getOtherPara().length; i++) {
            if(i > 0) builder.append(",");
            builder.append(brandModel.getOtherPara()[i]);
        }
        brand.setOtherPara(builder.toString());
        return brand;
    }

    public static SpecModel spec2Model(LedSpec spec){
        if(spec == null) return null;
        SpecModel specModel = new SpecModel();
        specModel.setId(spec.getId());
        specModel.setBid(spec.getBid());
        specModel.setName(spec.getName());
        specModel.setScanning(spec.getScanning());
        specModel.setSpacing(spec.getSpacing());
        specModel.setResPower(spec.getResPower());
        specModel.setSize(spec.getSize());
        specModel.setDensity(spec.getDensity());
        specModel.setWeight(spec.getWeight());
        specModel.setBrightness(spec.getBrightness());
        specModel.setElectric(spec.getElectric());
        specModel.setWaste(spec.getWaste());
        specModel.setDistance(spec.getDistance());
        specModel.setType(spec.getType());
        specModel.setDrive(spec.getDrive());
        specModel.setRefresh(spec.getRefresh());
        HashMap map = JSON.parseObject(spec.getOtherPara(), HashMap.class);
        specModel.setOtherPara(map);
        return specModel;
    }

    public static LedSpec model2Spec(SpecModel specModel){
        if(specModel == null) return null;
        LedSpec spec = new LedSpec();
        spec.setId(specModel.getId());
        spec.setBid(specModel.getBid());
        spec.setName(specModel.getName());
        spec.setScanning(specModel.getScanning());
        spec.setSpacing(specModel.getSpacing());
        spec.setResPower(specModel.getResPower());
        spec.setSize(specModel.getSize());
        spec.setDensity(specModel.getDensity());
        spec.setWeight(specModel.getWeight());
        spec.setBrightness(specModel.getBrightness());
        spec.setElectric(specModel.getElectric());
        spec.setWaste(specModel.getWaste());
        spec.setDistance(specModel.getDistance());
        spec.setType(specModel.getType());
        spec.setDrive(specModel.getDrive());
        spec.setRefresh(specModel.getRefresh());
        String s = JSON.toJSONString(specModel.getOtherPara());
        spec.setOtherPara(s);
        return spec;
    }
}
